<div class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">取酒编号：</label>
            <div class="layui-input-inline seller-inline-3">
                <input type="text" name="order_bn"  placeholder="请输入取酒编号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm"  lay-submit lay-filter="payments-search"><i class="iconfont icon-chaxun"></i>筛选</button>
            <button type="button" class="layui-btn layui-btn-sm add-liquor"><i class="layui-icon">&#xe608;</i> 存酒</button>
        </div>
    </div>
</div>
<div class="table-body">
    <div class="layui-tab layui-tab-brief">
        <ul class="layui-tab-title" id="tab">
            <li class="layui-this" data-types="deposit">未出单</li>
            <li data-types="">所有取酒</li>
        </ul>
    </div>
    <table id="paymentsSellerTable" lay-filter="paymentsSellerTable"></table>
</div>
<script>
    layui.use(['table','form','layer', 'laydate'], function(){
        let lock = false;
        var table = layui.table,form = layui.form,layer = layui.layer;
        var type='deposit'
        //第一个实例
        table.render({
            elem: '#paymentsSellerTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-260',
            cellMinWidth: '80',
            page:true,
            limit:70,
            id:'paymentsSellerTable',
            url: "{:url('api')}",
            where: {status: type},
            cols: [[ //表头
                {type:'numbers'},
                {field: 'order_bn',title:'订单编号',align:'center',width:100},
                {field: 'nickname',title: '取酒人',align:'center',minWidth:150, templet: function(data){
                    return '<span>'+data.user.nickname+'</span>'; 
                }},
                {field: 'mobiles', title: '手机号码',align:'center',width:120, templet: function(data){
                    return '<span>'+data.user.mobile+'</span>'; 
                }},
                {title: '商品名称',align:'center',minWidth:150, templet: function(data){
                    return '<span>'+data.liquor.goods_name+'</span>'; 
                }},
                {field: 'status', title: '状态',align:'center',width:100,templet: function(data){
                    switch(data.status){
                        case 0:
                            return '<span>待出单</span>';
                        case 1:
                            return '<span>已出单</span>';
                        case 2:
                            return '<span>已取消</span>'; 
                        default: 
                            return '<span>非法</span>'; 
                    }
                }},
                {field: 'number', title: '取酒数量',align:'center',width:100},
                {field: 'room_bn', title: '房间号',align:'center',width:100},
                { title: '存酒单号',align:'center',width:100, templet: function(data){
                    return '<span>'+data.liquor.liquor_bn+'</span>'; 
                }},
                { title: '存酒数量',align:'center',width:100, templet: function(data){
                    return '<span>'+data.liquor.number+'</span>'; 
                }},
                {field: 'ctime', title: '订单时间',align:'center',width:170, templet: function(data){
                    var timestamp4 = new Date(data.ctime*1000);
                    return timestamp4.toLocaleDateString().replace(/\//g, "-") + " " + timestamp4.toTimeString().substr(0, 8); 
                }},
                {fixed: 'right', width:170,title:'操作', align:'center', toolbar: '#payments'}
            ]]
        });
        form.on('submit(payments-search)', function(data){
            table.reload('paymentsSellerTable', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        $('#tab li').click(function() {
            type = $(this).data('types');
            table.reload('paymentsSellerTable', {
                where: {status: type}
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        })


        //监听工具条
        table.on('tool(paymentsSellerTable)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if(layEvent === 'edit') { //编辑
                layer.confirm('确定要出单?',{icon: 3,title:'提示'}, function(){
                    JsPost("{:url('issue')}",{id: data.id}, function(res) {
                        if(res.status){
                            layer.msg(res.msg,{time:1300},function(){
                                table.reload('paymentsSellerTable');
                            });
                        }else{
                            layer.msg(res.msg);
                        }
                    })
                });
            }else if(layEvent == 'cancel'){
                layer.confirm('真的要取消吗',{icon: 3,title:'提示'}, function(){
                    JsPost("{:url('cancel')}",{id: data.id}, function(res) {
                        if(res.status){
                            layer.msg(res.msg,{time:1300},function(){
                                table.reload('paymentsSellerTable');
                            });
                        }else{
                            layer.msg(res.msg);
                        }
                    });
                });
            } else if(layEvent === 'dy') {
                $.ajax({
                    url:"{:url('getLiquorOrder')}",
                    type:'post',
                    data: {id:data.id},
                    success:function(res){
                        if(res.status == true){
                            let date = new Date();
                            let year = date.getFullYear();
                            let month = date.getMonth() + 1;
                            let day = date.getDate();
                            let hour = date.getHours();
                            let minute = date.getMinutes();

                            let liquor = res.data;
                            console.log(liquor);

                            LODOP = getLodop(document.getElementById('LODOP1'), document.getElementById('LODOP_EM1'));
                            LODOP.PRINT_INITA(0, 0, 230, 230, "打印条码");
                            LODOP.SET_PRINTER_INDEX(-1);
                            LODOP.SET_PRINT_PAGESIZE(1,580,-1,"");
                            LODOP.SET_PRINT_STYLE("FontSize", 12);
                            LODOP.SET_PRINT_STYLE("Bold", 1);
                            LODOP.ADD_PRINT_TEXT("30","30","230","20","取酒单");
                            LODOP.SET_PRINT_STYLE("FontSize", 12);
                            LODOP.SET_PRINT_STYLE("Bold", 1);
                            LODOP.ADD_PRINT_TEXT(70, 10, 230, 20, "房间编号：" + liquor.room_bn);
                            LODOP.ADD_PRINT_TEXT(100, 10, 300, 20, "取酒时间：" + liquor.ctime2);
                            LODOP.ADD_PRINT_TEXT(130, 10, 230, 20, "下单人：" + liquor.user.nickname);
                            LODOP.SET_PRINT_STYLE("FontSize", 10);
                            LODOP.SET_PRINT_STYLE("Bold", 1);
                            /*if(booking.taocan && typeof (booking.taocan) == 'object'){
                                LODOP.ADD_PRINT_TEXT(90, 10, 230, 20, "套餐：" + booking.taocan['name'] + "  -  ￥" + toDecimal2NoZero(Number(booking.price )));
                            }else{
                                LODOP.ADD_PRINT_TEXT(90, 10, 230, 20, "套餐：-");
                            }*/
                            LODOP.ADD_PRINT_TEXT(150, 10, 230, 20, "下单电话：" + liquor.user.mobile);
                            let x = 180
                            LODOP.SET_PRINT_STYLE("FontSize", 10);
                            LODOP.SET_PRINT_STYLE("Bold", 0);
                            
                            let nk = "";
                            if(strlen(liquor.liquor.goods_name) <= 24){
                                let y = 24 - strlen(liquor.liquor.goods_name);
                                for(let a = 0; a < y; a++){
                                    nk += " ";
                                }
                            }else{
                                liquor.liquor.goods_name = liquor.liquor.goods_name.substring(0,21) + "..";
                                nk += "  ";
                            }
                            LODOP.ADD_PRINT_TEXT(x, 10, 230, 20,  liquor.liquor.goods_name + nk + "x" + liquor.number);
                            x += 20
                            LODOP.SET_PRINT_STYLE("FontSize", 9);
                            LODOP.SET_PRINT_STYLE("Bold", 1);
                            LODOP.ADD_PRINT_TEXT(x + 10, 10, 230, 20, "总金额：￥" + toDecimal2NoZero(Number(0)));

                            LODOP.SET_PRINT_STYLE("FontSize", 8);
                            LODOP.SET_PRINT_STYLE("Bold", 0);
                            LODOP.ADD_PRINT_TEXT(x + 30, 10, 230, 20, "打印时间：" + year + "/" + month + "/"+ day + " " + hour + ":"+ minute);
                            LODOP.SET_PRINT_STYLE("FontSize", 10);
                            LODOP.SET_PRINT_STYLE("Bold", 1);
                            //LODOP.ADD_PRINT_TEXT(x + 60, 10, 230, 20, "支付方式：无");
                            LODOP.SET_PRINT_STYLE("FontSize", 8);
                            LODOP.SET_PRINT_STYLE("Bold", 0);
                            LODOP.ADD_PRINT_TEXT(x + 90, 10, 230, 20, "-----------------------------------------");
                            LODOP.PREVIEW();
                        }else{
                            layer.msg(res.msg);
                        }
                    }
                },'json');
            }
        });
    });

    function toDecimal2NoZero(x) {
        let f = Math.round(x * 100) / 100;
        let s = f.toString();
        return s;
    }

    function strlen(str){
        let len = 0;
        for (let i=0; i<str.length; i++) {
            let c = str.charCodeAt(i);
            //单字节加1
            if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
                len++;
            }
            else {
                len+=2;
            }
        }
        return len;
    }
</script>

<script type="text/html" id="payments">
    {{# if(d.status=='0') { }}
    <a class="layui-btn layui-btn-xs" lay-event="edit">出单</a>
    <a class="layui-btn layui-btn-xs" lay-event="dy">打印</a>
    {{# } }}
    
    {{# if(d.status != '2') { }}
    <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancel">取消</a>-->
    {{# } }}
</script>